package 富婆梦.第一阶段.第五周.作业.俄罗斯转盘;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第五周.作业.俄罗斯转盘
 * @ClassName: TestGame
 * @Author: hemengjin
 * @Description: 游戏开始类
 * @Date:2021/8/22 18:23
 * @Version: 1.0
 */
public class TestGame {
    int count = 0;//开枪次数统计
    //四个玩家
    static LinkedList<Player> players = new LinkedList<>();
    LinkedList<Player> player = new LinkedList<>();

    public static void main(String[] args) {
        TestGame test = new TestGame();
        test.players.add(new Player("小A"));
        test.players.add(new Player("小B"));
        test.players.add(new Player("小C"));
        test.players.add(new Player("小D"));
        test.player.addAll(test.players);
        Loop lo = new Loop();

        //枪里面随机放3颗子弹
        Gun.test1();
        //转动转轮
        Gun.turn();
        System.out.println("游戏开始前：");
        for (Player p1 : test.players) {
            System.out.println(p1.getName() + "是否活着" + p1.isLife());
        }

        System.out.println("--------游戏正式开始--------");

        while (true) {
            test.takeTurn(test.player);
            if (Gun.getBullet()==0){
                System.out.println("-------------------游戏结束--------------");
                for (Player p1 : test.players) {
                    System.out.println(p1.getName() + "是否活着" + p1.isLife());
                }
                break;
            }
        }
    }
    //玩家列表中的玩家轮流对着自己开枪，一旦有人死，则需要更新，玩家列表
    public void takeTurn(LinkedList<Player> player){
        LinkedList<Player> temp = new LinkedList<>();
        for (int i = 0; i < player.size(); i++) {
            count=count+1;
            if (player.get(i).fire() == true) {
                //中枪死掉了
                TestGame.death(player.get(i));
                System.out.println("这是第"+count+"枪，开枪者为"+player.get(i).getName()+"死掉了，"+"还剩下"+Gun.getBullet()+"发子弹");
                temp.addAll(Loop.queue(player, i));
                player.clear();
                player.addAll(temp);
                break;
            }else {
                System.out.println("这是第"+count+"枪，开枪者为"+player.get(i).getName()+"活下来了，"+"还剩下"+Gun.getBullet()+"发子弹");
            }
        }
    }

    public static void death(Player p){
        //中枪的人需要修改生命状态
        for (Player pl:players) {
            if (pl.getName()==p.getName()){
                pl.gotShoot();
            }
        }
    }
}
